<?php

namespace App\Remote\XiaoKe;

use Closure;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

class Remote extends \App\Remote\Remote
{

    public static function getBaseUri(): string
    {
        // TODO: Implement getBaseUri() method.
        return "https://api.oem.xiaoke.cn";
    }

    public static function buildBasicHeaders(): array
    {
        $ts  = substr(array_sum(explode(' ', microtime())) * 1000, 0, 13);
        $ign = base64_encode(hash_hmac("sha1", $ts, "8435e330f4994d128fa7576f8c105673", true));
        return [
            "Content-Type" => "application/json",
            "X-AK-KEY"     => "9d3fc6b92958419299e28f5e2ad54428",
            "X-AK-TS"      => $ts,
            "X-AK-PIN"     => $ign
        ]; // TODO: Change the autogenerated stub
    }

    /**
     * @return Closure
     */
    public static function registerResponseHandle(): Closure
    {
        return function ($responseHttpCode, $responseOriginalContents) {
            if ($responseHttpCode == ResponseAlias::HTTP_OK && isJson($responseOriginalContents)) {
                $responseArr = jsonToArr($responseOriginalContents);
                if (in_array($responseArr["code"]["errcode"], [0, "87001008000104"])) {
                    return $responseArr["data"] ?? [];
                }
            }
            throw new \Exception($responseHttpCode . "-" . $responseOriginalContents);
        }; // TODO: Change the autogenerated stub
    }

    /**
     * @return string
     */
    public static function getWid(): string
    {
        return "3188942251";
    }

    /**
     * @return string
     */
    public static function getPid(): string
    {
        return "4019971370148";
    }
}
